package com.minutes.in.seconds.common;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class BitmapHandler {

	public static Bitmap getCroppedBitmap(Bitmap bitmap, int size) {
		Bitmap output;
		if (bitmap.getWidth() >= bitmap.getHeight()) {

			output = Bitmap.createBitmap(bitmap, bitmap.getWidth() / 2 - bitmap.getHeight() / 2, 0, bitmap.getHeight(), bitmap.getHeight());

		} else {

			output = Bitmap.createBitmap(bitmap, 0, bitmap.getHeight() / 2 - bitmap.getWidth() / 2, bitmap.getWidth(), bitmap.getWidth());
		}

		Bitmap mutableBitmap = Bitmap.createBitmap(output.getWidth(), output.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(mutableBitmap);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, output.getWidth(), output.getHeight());

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawCircle(output.getWidth() / 2, output.getHeight() / 2, output.getWidth() / 2, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(output, rect, rect, paint);
		return Bitmap.createScaledBitmap(mutableBitmap, size, size, true);
	}

}
